///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWebCollection.ts'/>
///<reference path='SPRESTUtil.ts'/>

// Module
module SPREST {

    // Class
    export class Site {
        public contextInfoSiteUrl: string = null;
        public started: bool = false;
        public ready: bool = false;
        public dfd: JQueryDeferred = $.Deferred();

        public urlEventReceiver: string = null;
        public urlFeatures: string = null;
        public urlOwner: string = null;
        public urlRecycleBin: string = null;
        public urlRootWeb: string = null;
        public urlUserCustomActions: string = null;
        public allowDesigner: bool = null;
        public allowMasterPageEditing: bool = null;
        public allowRevertFromTemplate: bool = null;
        public allowSelfServiceUpgrade: bool = null;
        public allowSelfServiceUpgradeEvaluation: bool = null;
        public compatibilityLevel: number = null;
        public id: string = null;
        public maxItemsPerThrottledOperation: number = null;
        public primaryUri: string = null;
        public readOnly: bool = null;
        public serverRelativeUrl: string = null;
        public shareByLinkEnabled: bool = null;
        public showUrlStructure: bool = null;
        public uIVersionConfigurationEnabled: bool = null;
        public upgradeReminderDate: string = null;
        public upgrading: bool = null;
        public url: string = null;

        // Constructor
        constructor (public Context:SPREST.Context) {
        }

        GetWebs():SPREST.WebCollection {
            var webs: SPREST.WebCollection = new SPREST.WebCollection(this.Context, this);
            var dfd: JQueryDeferred = webs.start();
            return webs;
        }

        GetWeb(): SPREST.Web {
            var web: SPREST.Web = new SPREST.Web(this.Context, this, this.Context.contextWebUrl);
            var dfd:JQueryDeferred = web.start();
            return web;
        }

        start(): JQueryDeferred {
            var myThis: Site = this;
            if (myThis.started) return myThis.dfd;
            myThis.started = true;
            var siteInfoUrl:string = Util.concatenateUrl(this.Context.contextSiteUrl, SPREST.Constants.SiteApiUrl);
            $.ajax(siteInfoUrl, {
                async: true,
                headers: { "Accept":"application/json;odata=verbose" },
                type: "GET"
            }).fail(function(req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
            
                myThis.urlEventReceiver = data.d.EventReceivers.__deferred.uri;
                myThis.urlFeatures = data.d.Features.__deferred.uri;
                myThis.urlOwner = data.d.Owner.__deferred.uri;
                myThis.urlRecycleBin = data.d.RecycleBin.__deferred.uri;
                myThis.urlRootWeb = data.d.RootWeb.__deferred.uri;
                myThis.urlUserCustomActions = data.d.UserCustomActions.__deferred.uri;
                myThis.allowDesigner = data.d.AllowDesigner;
                myThis.allowMasterPageEditing = data.d.AllowMasterPageEditing;
                myThis.allowRevertFromTemplate = data.d.AllowRevertFromTemplate;
                myThis.allowSelfServiceUpgrade = data.d.AllowSelfServiceUpgrade;
                myThis.allowSelfServiceUpgradeEvaluation = data.d.AllowSelfServiceUpgradeEvaluation;
                myThis.compatibilityLevel = data.d.CompatibilityLevel
                myThis.id = data.d.Id;
                myThis.maxItemsPerThrottledOperation = data.d.MaxItemsPerThrottledOperation;
                myThis.primaryUri = data.d.PrimaryUri;
                myThis.readOnly = data.d.ReadOnly;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.shareByLinkEnabled = data.d.ShareByLinkEnabled;
                myThis.showUrlStructure = data.d.ShowUrlStructure;
                myThis.uIVersionConfigurationEnabled = data.d.UIVersionConfigurationEnabled;
                myThis.upgradeReminderDate = data.d.UpgradeReminderDate;
                myThis.upgrading = data.d.Upgrading;
                myThis.url = data.d.Url;

                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });

            return myThis.dfd;
        }
    }

}